package com.jic.manager.impl;

import com.jic.common.base.vo.PageResult;
import com.jic.manager.mapper.SysConfigMapper;
import com.jic.manager.entity.SysConfig;
import com.jic.conf.request.SysConfigRequest;
import com.jic.manager.service.SysConfigService;
import com.jic.manager.utils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 全局参数表
 * @author : 
 * @email : 
 * @since : 2020-02-24 03:11:07
 * @version : v1.0.0
 */
@Service
public class SysConfigServiceImpl implements SysConfigService {

    @Autowired
    private SysConfigMapper sysConfigMapper;

    @Override
    public long insert(SysConfigRequest sysConfigRequest) throws Exception {
        SysConfig sysConfig = new SysConfig();
        BeanUtils.Copy(sysConfigRequest,sysConfig);
        sysConfig.setCreator(null!=sysConfigRequest.getAdminOperate()?sysConfigRequest.getAdminOperate().getOperator():0);
        sysConfig.setCreatorName(null!=sysConfigRequest.getAdminOperate()?sysConfigRequest.getAdminOperate().getOperateName():"");
        sysConfig.setDeleteFlag(0);
        long flag = sysConfigMapper.insert(sysConfig);
        return flag;
    }

    @Override
    public long delete(SysConfigRequest sysConfigRequest) throws Exception {
        SysConfig sysConfig = new SysConfig();
        BeanUtils.Copy(sysConfigRequest,sysConfig);
        String ids = sysConfigRequest.getIds();
        if (StringUtils.isEmpty(ids)){
            throw new Exception("必传参数为空 ids");
        }
        String[] idsStr = ids.split(",");
        for (int i = 0; i < idsStr.length; i++) {
            sysConfig.setId(Long.valueOf(idsStr[i]));
            sysConfigMapper.delete(sysConfig);
        }
        return 0000;
    }

    @Override
    public long update(SysConfigRequest sysConfigRequest) throws Exception {
        SysConfig sysConfig = new SysConfig();
        BeanUtils.Copy(sysConfigRequest,sysConfig);
        sysConfig.setUpdater(null!=sysConfigRequest.getAdminOperate()?sysConfigRequest.getAdminOperate().getOperator():0);
        sysConfig.setUpdateName(null!=sysConfigRequest.getAdminOperate()?sysConfigRequest.getAdminOperate().getOperateName():"");
        sysConfig.setDeleteFlag(0);
        return sysConfigMapper.update(sysConfig);
    }

    @Override
    public SysConfig queryByPrimaryKey(Long id){
        return sysConfigMapper.queryByPrimaryKey(id);
    }

    @Override
    public List<SysConfig> query(SysConfig sysConfig){
        return sysConfigMapper.query(sysConfig);
    }

    @Override
    public PageResult<SysConfig> queryPage(SysConfigRequest sysConfigRequest) throws Exception {
        //计算下标
        int startIndex = (sysConfigRequest.getStart() - 1) * sysConfigRequest.getLimit();
        SysConfig sysConfig = new SysConfig();
        BeanUtils.Copy(sysConfigRequest,sysConfig);
        sysConfig.setDeleteFlag(0);

        List<SysConfig> list = sysConfigMapper.queryPage(startIndex, sysConfigRequest.getLimit(), sysConfig);
        long count = sysConfigMapper.queryPageCount(sysConfig);
        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return pageResult;
    }

    @Override
    public long updateCommonList(SysConfigRequest sysConfigRequest) {
        return sysConfigMapper.updateCommonList(sysConfigRequest);
    }
}